//任意数阶乘
function factorial(n) {
    if (n <= 0) {
        return 1;
    }
    return n * factorial(n - 1);
}



//时间差
function leadTime(time1, time2) {
    var differ = parseInt(Math.abs(time1.getTime() - time2.getTime()) / 1000);
    var day = parseInt(differ / (60 * 60 * 24));
    var hours = parseInt(differ % (60 * 60 * 24) / (60 * 60));
    var minutes = parseInt(differ % (60 * 60) / 60);
    var seconds = parseInt(differ % 60);
    var obj = {};
    obj.day = day;
    obj.hours = hours;
    obj.minutes = minutes;
    obj.seconds = seconds;
    return obj;
}



//两个数字间的随机数,包小也包大;
function randomNum(n1, n2) {
    let maxNum = Math.max(n1, n2);
    let minNum = Math.min(n1, n2);
    return parseInt(Math.random() * (maxNum - minNum + 1) + minNum);
}



//cookie增、改
//语法：document.cookie = name=value[;expires=date][;path=路径][;domain=域名]
function setCookie(key, val, iDay) {
    var str = key + '=' + val + ';path=/';
    if (iDay) {
        var date = new Date();
        date.setDate(date.getDate() + iDay);
        str += ';expires=' + date;
    }
    document.cookie = str;
}



//cookie查(获取)
function getCookie(key) {
    var str = getCookie;
    var arr = str.split('; ');
    for (var ele of arr) {
        var arr2 = ele.split('=');
        if (key == arr2[0]) {
            return arr2[1];
        }
    }
}



//cookie删
function removeCookie(key) {
    setCookie(key, '', -1);
}



// 多属性运动函数
function move(ele, target, fn = () => { }) {
    let timerObj = {};
    for (let key in target) {
        timerObj[key] = setInterval(function () {
            let current = parseInt(window.getComputedStyle(ele)[key]);
            let distance = (target[key] - current) / 10;
            distance = distance > 0 ? Math.ceil(distance) : Math.floor(distance);
            if (current == target[key]) {
                clearInterval(timerObj[key]);
                delete timerObj[key];
                let n = 0;
                for (let i in timerObj) n++;
                if (n === 0) fn();
            } else {
                ele.style[key] = distance + current + 'px';
            }
        }, 20)
    }
}
